module D{
    export class Net{
        private socket: egret.WebSocket;
        //private byte: egret.ByteArray;
        constructor() {
            //这里我们采用小端
            //this.byte.endian = egret.Endian.LITTLE_ENDIAN;
            this.socket = new egret.WebSocket();
            this.socket.type = egret.WebSocket.TYPE_BINARY; //设置后可以接收二进制数据
            this.socket.type = egret.WebSocket.TYPE_STRING; //设置后默认读取字符串
        }
        /** 建立连接 */
        public connect(url):void{
            console.log(url);
            this.socket.connectByUrl(url);
            this.socket.addEventListener(egret.Event.CONNECT, App.onConnect, App);
            this.socket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onMessage, this);
            this.socket.addEventListener(egret.Event.CLOSE, this.closeHandler, this);
            this.socket.addEventListener(egret.IOErrorEvent.IO_ERROR, this.errorHandler, this);
        }
        /** 关闭连接 */
        public close():void{
            this.socket.removeEventListener(egret.Event.CONNECT, App.onConnect, App);
            this.socket.removeEventListener(egret.ProgressEvent.SOCKET_DATA, App.onMessage, App);
            this.socket.removeEventListener(egret.Event.CLOSE, this.closeHandler, this);
            this.socket.removeEventListener(egret.IOErrorEvent.IO_ERROR, this.errorHandler, this);
            this.socket.close();
        }
        /** 接收到消息 */
        public onMessage(e: egret.ProgressEvent):void{
            let result: any = this.socket.readUTF();
            if(result.length == 0){ //二进制消息只游戏消息使用
                let retBin:egret.ByteArray = new egret.ByteArray();
                this.socket.readBytes(retBin);
                retBin.endian = egret.Endian.LITTLE_ENDIAN;
                retBin.position = 4;
                let man = retBin.readUnsignedShort(), sub = retBin.readUnsignedShort();
                //console.log("R ["+man+","+sub+"]");
                if(man == 6){
                    if(sub == 101) App.gameView && App.gameView.onSyncData(retBin);
                    else if(sub == 255){
                        let jd = App.b2j(retBin, [["S","MT"],["S","ET"],["I","CC"],["I","EV"],["S","ML"]]);
                        console.log(jd);
                    }
                }else{
                    App.gameView && App.gameView.onNetData(sub, retBin);
                }
            }else{
                console.log("R", result);
                result = JSON.parse(result);
                App.onMessage(result);
            }
        }
        /** 发送数据 */
        public send(d:any):void{
            if(egret.is(d, "egret.ByteArray")){
                console.log("SB", d.length);
                this.socket.writeBytes(d);
            }else{
                if(typeof(d) == "object") d = JSON.stringify(d);
                console.log("S", d);
                this.socket.writeUTF(d);
                this.socket.flush();
            }
        }
        /** 网络断开 */
        private closeHandler(e: any = null): void {
            //关闭事件
            console.log("socket close", e);
        }
        /** 网络出错了 */
        private errorHandler(e: any = null): void {
            //连接出错
            console.log("socket error", e);
        }
    }
}